<?php
//var_dump($_REQUEST);
//die();
require_once 'includes/php/connect_db.php';
session_start();
$fields = array('date_', 'dev_type_', 'dev_progress_', 'german_status_', 'english_status_', 'user_');
$tech_users = array();
$sql = "SELECT * FROM tbl_0962_technical_user ORDER BY order_idx";
if ( !$res = mysql_query($sql) ) {
    die("Error in line " . __LINE__ . ":<br>" . mysql_error());
}
while ( $row = mysql_fetch_assoc($res) ) {
    $tech_users[$row['user_id']]['name'] = $row['user_name'];
    $tech_users[$row['user_id']]['bg_color'] = $row['bg_color'];
}
if ( $_REQUEST['action'] == 'save' ) {
    $data = array();
    foreach ( $_REQUEST as $key => $value ) {
        foreach ( $fields as $field_prefix ) {
            if ( strpos($key, $field_prefix) !== false ) {
                $data[substr($key, strlen($field_prefix))][$field_prefix] = $value;
            }
        }
    }
//    var_dump($data);
    foreach ( $data as $rec_id => $value ) {
        $sql = "SELECT 1 FROM tbl_0961_page_devel_status WHERE page_list_id = '$rec_id'";
        if ( !$res = mysql_query($sql) ) {
            die("Error in line " . __LINE__ . ":<br>" . mysql_error());
        }
        if ( mysql_num_rows($res) > 0 ) {
            $sql = "
            UPDATE tbl_0961_page_devel_status
            SET date = '" . ($value['date_'] != 0 && $value['date_'] != '' ? strtotime($value['date_']) : 0) . "',
                develop_type = '{$value['dev_type_']}',
                develop_progress = '{$value['dev_progress_']}',
                german_status = '{$value['german_status_']}',
                english_status = '{$value['english_status_']}',
                user = '{$value['user_']}'
            WHERE page_list_id = '$rec_id'
            ";
        } else {
//            var_dump($value['date_']);
            $sql = "
            INSERT INTO tbl_0961_page_devel_status
            SET date = '" . ($value['date_'] != '' && $value['date_'] != 0 ? strtotime($value['date_']) : 0) . "',
                develop_type = '{$value['dev_type_']}',
                develop_progress = '{$value['dev_progress_']}',
                german_status = '{$value['german_status_']}',
                english_status = '{$value['english_status_']}',
                page_list_id = '$rec_id',
                user = '{$value['user_']}'
            ";
        }
        if ( !mysql_query($sql) ) {
            die("Error in line " . __LINE__ . ":<br>" . mysql_error() . "<br>" . $sql);
        }
    }
    header("Location: development_status.php");
    die();
}
if ( $_REQUEST['sorted'] ) {
    $_SESSION['sorted'] = $_REQUEST['sorted'];
    $_SESSION['sort_direction'] = $_REQUEST['sort_direction'];
}
    $_SESSION['filter'] = $_REQUEST['filter'];
switch ($_SESSION['sorted']) {
    case 'Page':
        $sorted = 'pl.page_name';
        break;
    case 'Type':
        $sorted = 'TYPE_SORT';
        break;
    case 'Page dev-stage':
        $sorted = 'ps.develop_type';
        break;
    case 'Develop. Progr.':
        $sorted = 'ps.develop_progress';
        break;
    case 'German progress':
        $sorted = 'ps.german_status';
        break;
    case 'English progress':
        $sorted = 'ps.english_status';
        break;
    case 'User':
        $sorted = 'ps.user';
        break;
    case 'Date':
        $sorted = 'ps.date';
        break;
    case 'Variable Name':
        $sorted = 'pl.str_var_prefix';
        break;

    default:
        $sorted = 'pl.page_name';
        break;
}

switch ($_SESSION['sort_direction']) {
    case 'Asc':
        $direction = 'ASC';
        break;
    case 'Desc':
        $direction = 'DESC';
        break;
    default:
        $direction = 'ASC';
        break;
}
$sql = "
    SELECT pl.*, ps.*,
    IF (i_type_app_page = 2, 1, IF (i_type_app_page = 1, 2, 3)) TYPE_SORT
    FROM tbl_0950_page_list pl
    LEFT JOIN tbl_0961_page_devel_status ps ON (ps.page_list_id = pl.lng_k_page_list)
    ".($_SESSION['filter'] > 0?"WHERE ps.user = {$_SESSION['filter']}":"")."
    ORDER BY $sorted $direction
";

if ( !$res = mysql_query($sql) ) {
    die("Error in line " . __LINE__ . ":<br>" . mysql_error());
}
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Pragma: no-cache');
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Growmetry Web Page Development and Translation Status</title>
        <script language=JavaScript src="js/development_status.js"></script>
        <style>
            h1 {
                font-family: Arial;
                font-size: 20px;
                text-align: center;
            }
            .centered {
                text-align: center;
            }
            .right {
                text-align: right;
            }
            .wrapped {
                white-space: nowrap; 
                text-overflow: ellipsis; 
                overflow: hidden;
            }
            .table_1 {
                width: 1000px;
                margin-bottom: 10px;
            }
            .table_2 {
                font-family: Arial;
                border-collapse: collapse;
            }
            .table_2 th {
                color: white;
                background-color: black;
                border: 2px solid #000;
                text-align: center;
                font-size: 14px; 
                font-weight: bold;
            }
            .table_2 td {
                padding: 0px;
                /*                border: 2px solid #ccc;*/
            }
            .grey {
                background-color: #c2ceda;
            }
            a {
                /*text-decoration: none;*/
                color: #000;
            }

            a:hover {
                /*text-decoration: none;*/
                color: #000;
            }


        </style>
    </head>
    <body>
        <h1 style="width: 1000px;">Growmetry Web Page Development and Translation Status</h1>
        <table class="table_1">
            <tr>
                <td width="40%">&nbsp;</td>
                <td width="20%">
                    <div id="divNotSaved" style="color: red; display: none; font-family: Arial; font-weight: bold; font-size: 130%;">Not Saved!</div>
                </td>
                <td width="40%" style="text-align: right;">
                    <input style="width: 90px;" id="btnSave" type="button" value="Save" disabled="" onclick="form1.submit();" />
                </td>
            </tr>
            <tr>
                <td colspan="3" style="text-align: left; font-family: Arial; padding-top: 10px;">
                    <form method="post" name="main_form">
                        Sort by:
                        <select name="sorted" id="sorted">
                            <option <?= $_SESSION['sorted'] == 'Type' ? 'selected' : '' ?>>Type</option>
                            <option <?= $_SESSION['sorted'] == '' || $_SESSION['sorted'] == 'Page' ? 'selected' : '' ?>>Page</option>
                            <option <?= $_SESSION['sorted'] == 'Page dev-stage' ? 'selected' : '' ?>>Page dev-stage</option>
                            <option <?= $_SESSION['sorted'] == 'Develop. Progr.' ? 'selected' : '' ?>>Develop. Progr.</option>
                            <option <?= $_SESSION['sorted'] == 'German progress' ? 'selected' : '' ?>>German progress</option>
                            <option <?= $_SESSION['sorted'] == 'English progress' ? 'selected' : '' ?>>English progress</option>
                            <option <?= $_SESSION['sorted'] == 'User' ? 'selected' : '' ?>>User</option>
                            <option <?= $_SESSION['sorted'] == 'Date' ? 'selected' : '' ?>>Date</option>
                            <option <?= $_SESSION['sorted'] == 'Variable Name' ? 'selected' : '' ?>>Variable Name</option>
                        </select>
                        <select name="sort_direction" id="sort_direction">
                            <option <?= $_SESSION['sort_direction'] == '' || $_SESSION['sort_direction'] == 'Asc' ? 'selected' : '' ?>>Asc</option>
                            <option <?= $_SESSION['sort_direction'] == 'Desc' ? 'selected' : '' ?>>Desc</option>
                        </select>
                        <input type="submit" value="Sort" />
                        <input type="hidden" name="action" value="sorting" />
                        <span style="margin-left: 30px;">Filter by: </span>
                        <select name="filter" id="filter" style="background-color: #<?= $tech_users[$_SESSION['filter']]['bg_color'] ?>" onchange="paint_combo(this); document.main_form.submit()">
                            <?
                            foreach ( $tech_users as $user_id => $user ) {
                            ?>
                            <option value="<?= $user_id ?>" <?= $_SESSION['filter'] == $user_id ? 'selected' : '' ?> style="background-color: #<?= $user['bg_color']?>"><?= $user['name'] ?></option>
                            <?
                            }
                            ?>
                            <option value="0" <?= $_SESSION['filter'] == 0 ? 'selected' : '' ?> style="background-color: #FFF">no user</option>
                        </select>
                    </form>
                </td>
            </tr>
        </table>
        <form method="post" name="form1">
            <input type="hidden" value="save" name="action" />
            <table class="table_2">
                <tr>
                    <th style="width: 60px;">Type</th>
                    <th style="width: 233px;">Page</th>
<!--                    <th style="width: 15px;">&nbsp;</th>-->
                    <th style="width: 105px;"><div style="padding-left: 15px;">Page<br />dev-stage</div></th>
<!--                    <th style="width: 15px;">&nbsp;</th>-->
                <th style="width: 80px;"><div style="padding-left: 15px;">Develop.<br />Progr.</div></th>
<!--                    <th style="width: 15px;">&nbsp;</th>-->
                <th style="width: 80px;"><div style="padding-left: 15px;">German<br />progress</div></th>
<!--                    <th style="width: 15px;">&nbsp;</th>-->
                <th style="width: 80px;"><div style="padding-left: 15px;">English<br />progress</div></th>
<!--                    <th style="width: 15px;">&nbsp;</th>-->
                <th style="width: 100px;">User</th>
                <th style="width: 120px;">Date<br />Time</th>
                <th style="width: 130px;">Variable Name</th>
                </tr>
                <?
                $i = true;
                while ( $row = mysql_fetch_assoc($res) ) {
                    $i = !$i;
                    $type = ($row['i_type_app_page'] == 1 ? 'Home' : ($row['i_type_app_page'] == 2 ? 'Account' : 'System'));
                    ?>
                    <tr class="<?= $i ? "grey" : "" ?>">
                        <td style="font-size: 12px; padding-left: 4px;">
                            <input type="hidden" <?= "id='hidden_date_{$row['lng_k_page_list']}'" ?> name="date_<?= $row['lng_k_page_list'] ?>" value="<?= $row['date'] != 0 ? strftime("%d.%m.%Y %H:%M", $row['date']) : 0 ?>" />
                            <?= $type ?>
                        </td>
                        <td style="font-size: 14px; font-weight: bold; text-align: right;">
                            <a target="_blank" <?= 'href="translation_04.php?action=load&page_name=' . $row['lng_k_page_list'] . '"' ?>><?= $row['page_name'] ?></a>
                        </td>
    <!--                        <td class="right">
                            &nbsp;
                        </td>-->
                        <td class="right" style="padding-top: 1px; padding-bottom: 1px;">
                            <img style="display: none; top: 3px;" <?= "id='changed_dev_type_{$row['lng_k_page_list']}'" ?> src="images/red_bullet.png" />
                            <select style="font-size: 12px;" name="dev_type_<?= $row['lng_k_page_list'] ?>" onchange="cbxOnChange(this, '<?= $row['lng_k_page_list'] ?>');">
                                <option value="1" <?= $row['develop_type'] == 1 ? "selected" : "" ?>>Develop</option>
                                <option value="2" <?= $row['develop_type'] == 2 ? "selected" : "" ?>>Translat.</option>
                            </select>
                        </td>
    <!--                        <td class="right">
                            <img style="display: none;" <?= "id='changed_dev_progress_{$row['lng_k_page_list']}'" ?> src="images/red_bullet.png" />
                        </td>-->
                        <td class="right">
                            <img style="display: none;" <?= "id='changed_dev_progress_{$row['lng_k_page_list']}'" ?> src="images/red_bullet.png" />
                            <select style="font-size: 12px;" name="dev_progress_<?= $row['lng_k_page_list'] ?>" onchange="cbxOnChange(this, '<?= $row['lng_k_page_list'] ?>');">
                                <option value="0" <?= $row['develop_progress'] == 0 ? "selected" : "" ?>>0%</option>
                                <option value="10" <?= $row['develop_progress'] == 10 ? "selected" : "" ?>>10%</option>
                                <option value="20" <?= $row['develop_progress'] == 20 ? "selected" : "" ?>>20%</option>
                                <option value="30" <?= $row['develop_progress'] == 30 ? "selected" : "" ?>>30%</option>
                                <option value="40" <?= $row['develop_progress'] == 40 ? "selected" : "" ?>>40%</option>
                                <option value="50" <?= $row['develop_progress'] == 50 ? "selected" : "" ?>>50%</option>
                                <option value="60" <?= $row['develop_progress'] == 60 ? "selected" : "" ?>>60%</option>
                                <option value="70" <?= $row['develop_progress'] == 70 ? "selected" : "" ?>>70%</option>
                                <option value="80" <?= $row['develop_progress'] == 80 ? "selected" : "" ?>>80%</option>
                                <option value="90" <?= $row['develop_progress'] == 90 ? "selected" : "" ?>>90%</option>
                                <option value="100" <?= $row['develop_progress'] == 100 ? "selected" : "" ?>>100%</option>
                            </select>
                        </td>
    <!--                        <td class="right">
                            <img style="display: none;" <?= "id='changed_german_status_{$row['lng_k_page_list']}'" ?> src="images/red_bullet.png" />
                        </td>-->
                        <td class="right">
                            <img style="display: none;" <?= "id='changed_german_status_{$row['lng_k_page_list']}'" ?> src="images/red_bullet.png" />
                            <select style="font-size: 12px;" name="german_status_<?= $row['lng_k_page_list'] ?>" onchange="cbxOnChange(this, '<?= $row['lng_k_page_list'] ?>');">
                                <option value="0" <?= $row['german_status'] == 0 ? "selected" : "" ?>>0%</option>
                                <option value="10" <?= $row['german_status'] == 10 ? "selected" : "" ?>>10%</option>
                                <option value="20" <?= $row['german_status'] == 20 ? "selected" : "" ?>>20%</option>
                                <option value="30" <?= $row['german_status'] == 30 ? "selected" : "" ?>>30%</option>
                                <option value="40" <?= $row['german_status'] == 40 ? "selected" : "" ?>>40%</option>
                                <option value="50" <?= $row['german_status'] == 50 ? "selected" : "" ?>>50%</option>
                                <option value="60" <?= $row['german_status'] == 60 ? "selected" : "" ?>>60%</option>
                                <option value="70" <?= $row['german_status'] == 70 ? "selected" : "" ?>>70%</option>
                                <option value="80" <?= $row['german_status'] == 80 ? "selected" : "" ?>>80%</option>
                                <option value="90" <?= $row['german_status'] == 90 ? "selected" : "" ?>>90%</option>
                                <option value="100" <?= $row['german_status'] == 100 ? "selected" : "" ?>>100%</option>
                            </select>
                        </td>
    <!--                        <td class="right">
                            <img style="display: none;" <?= "id='changed_english_status_{$row['lng_k_page_list']}'" ?> src="images/red_bullet.png" />
                        </td>-->
                        <td class="right">
                            <img style="display: none;" <?= "id='changed_english_status_{$row['lng_k_page_list']}'" ?> src="images/red_bullet.png" />
                            <select style="font-size: 12px;" name="english_status_<?= $row['lng_k_page_list'] ?>" onchange="cbxOnChange(this, '<?= $row['lng_k_page_list'] ?>');">
                                <option value="0" <?= $row['english_status'] == 0 ? "selected" : "" ?>>0%</option>
                                <option value="10" <?= $row['english_status'] == 10 ? "selected" : "" ?>>10%</option>
                                <option value="20" <?= $row['english_status'] == 20 ? "selected" : "" ?>>20%</option>
                                <option value="30" <?= $row['english_status'] == 30 ? "selected" : "" ?>>30%</option>
                                <option value="40" <?= $row['english_status'] == 40 ? "selected" : "" ?>>40%</option>
                                <option value="50" <?= $row['english_status'] == 50 ? "selected" : "" ?>>50%</option>
                                <option value="60" <?= $row['english_status'] == 60 ? "selected" : "" ?>>60%</option>
                                <option value="70" <?= $row['english_status'] == 70 ? "selected" : "" ?>>70%</option>
                                <option value="80" <?= $row['english_status'] == 80 ? "selected" : "" ?>>80%</option>
                                <option value="90" <?= $row['english_status'] == 90 ? "selected" : "" ?>>90%</option>
                                <option value="100" <?= $row['english_status'] == 100 ? "selected" : "" ?>>100%</option>
                            </select>
                        </td>
    <!--                        <td class="right">
                            <img style="display: none;" <?= "id='changed_user_{$row['lng_k_page_list']}'" ?> src="images/red_bullet.png" />
                        </td>-->
                        <td class="right">
                            <img style="display: none;" <?= "id='changed_user_{$row['lng_k_page_list']}'" ?> src="images/red_bullet.png" />
                            <select style="width: 80px; font-size: 12px; background-color: #<?= $tech_users[$row['user']]['bg_color'] ?>" name="user_<?= $row['lng_k_page_list'] ?>" onchange="cbxOnChange(this, '<?= $row['lng_k_page_list'] ?>');">
                                <option value="0" <?= $row['user'] == 0 ? "selected" : "" ?> style="background-color: #FFF;">Select...</option>
                                <?
                                foreach ( $tech_users as $user_id => $user ) {
                                    ?>
                                <option value="<?= $user_id ?>" <?= $row['user'] == $user_id ? "selected" : "" ?> style="background-color: #<?= $user['bg_color']?>"><?= $user['name'] ?></option>
                                    <?
                                }
                                ?>
                            </select>
                        </td>
                        <td style="font-size: 10px;" class="centered">
                            <div <?= "id='div_date_{$row['lng_k_page_list']}'" ?>>
                                <?= $row['date'] != 0 ? strftime("%d.%m.%Y %H:%M", $row['date']) : "n/a" ?>
                            </div>
                        </td>
                        <td class="wrapped" style="font-size: 10px;">
                            <?= $row['str_var_prefix'] ?>
                        </td>
                    </tr>
                    <?
                }
                ?>
            </table>
            <table class="table_1">
                <tr>
                    <td width="40%">&nbsp;</td>
                    <td width="10%">&nbsp;</td>
                    <td width="40%" style="text-align: right;">
                        <input style="width: 90px;" id="btnSave2" type="button" value="Save" disabled="" onclick="form1.submit();" />
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

